from flask_restful import reqparse, request, Resource, Api, fields, marshal_with
from models import Student, db, User
from operator import or_, and_
from src.common.code import make_result
from src.common.code import Code
from src.common.hashliber import md5


class Register(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('userName', type=str, required=True, help='username缺失！')
        self.reqparse.add_argument('passWord', type=str, required=True, help='password缺失！')
        self.reqparse.add_argument('nickName', type=str, required=True, help='nickname缺失！')
        self.reqparse.add_argument('mobile', type=int, required=True, help='mobile缺失')
        self.args = self.reqparse.parse_args()

    # 根据提交的用户信息，写入数据库，密码经进md5加密后再写入。
    def post(self):
        user_name = self.args.get('userName', None)
        pass_word = self.args.get('passWord', None)
        nick_name = self.args.get('nickName', None)
        mobile = self.args.get('mobile', None)
        if user_name and pass_word and nick_name and mobile:
            user = User(user_name=user_name, pass_word=md5(pass_word), nick_name=nick_name, mobile=mobile)
            db.session.add(user)
            try:
                db.session.commit()
                return make_result()
            except:
                return make_result(code=Code.DATA_FAIL)
        else:
            return make_result(code=Code.PARAM_FAIL)


class IsExist(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('userName', type=str, required=True, help='username缺失！')
        self.args = self.reqparse.parse_args()

    # 根据提交的用户名，检测数据库，用户是否已存在。
    def get(self):
        user_name = self.args.get('userName', None)
        if user_name:
            user = User.query.filter(User.user_name == user_name).all()
            if user:
                return make_result(code=Code.USER_NAME_EXIST)
            else:
                return make_result()
        else:
            return make_result(code=Code.PARAM_FAIL)


class Login(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('userName', type=str, required=True, help='username缺失！')
        self.reqparse.add_argument('passWord', type=str, required=True, help='password缺失！')
        self.args = self.reqparse.parse_args()

    # 根据提交的用户和密码查询数据库，并返回用户信息以及token
    def get(self):
        user_name = self.args.get('userName', None)
        pass_word = self.args.get('passWord', None)

        if user_name and pass_word:
            try:
                user = User.query.filter(and_(User.user_name == user_name, User.pass_word == md5(pass_word))).all()
                if user:
                    result = []
                    for u in user:
                        result.append(u.to_json())
                    return make_result(data=result)
                else:
                    return make_result(code=Code.LOGIN_FAIL)
            except:
                return make_result(code=Code.DATA_FAIL)
        else:
            return make_result(code=Code.PARAM_FAIL)
